###***Moves mass within the disk to account for the change in angular speed of a bin due to any accretion by the embryo***###
from __future__ import division
from math import *
from Init_Cond import *

def f(x,y, R_Embryo, W):   #calculates new orbital speeds for bins, solves for mass movement to conserve L
 # 'x' is the mass of the embryo, y is inside
    M = x
    inside = y

    del w_new[:]
    del delm[:]

    for a in range(int(N)):
        w_new.append((G*M/r[a]**3)**0.5)

    for a in range(int(N)):
        if a < inside:
            delta = 0
        elif a == inside:
            delta = m[a]*R[a]*(w[a] - w_new[a])/(R_Embryo**2.0*W - R[a]*w_new[a])
        else:
            delta = m[a]*R[a]*(w[a] - w_new[a])/(R[a-1]*w_new[a-1] - R[a]*w_new[a])
        delm.append(delta)
        #print(w_new[a], w[a], delm[a])

    for a in range(int(N)):
        if delm[a] > m[a]:
            delm[a] = m[a]

def o(x, y):        #checks if first bin lost mass, adds it to mars accrete
    Mars_Accrete = x
    inside = y

    if delm[inside] > 0:
        return(Mars_Accrete + delm[inside])
    else:
        return(Mars_Accrete)

def g(x):       #moves the mass in the bins, according to delm[]
    inside = x
    # y = sum(sigma)
    for a in range(int(N)):

        # if int(inside-1) < a < int(N-1):
        if a == int(N-1):
            m[a] = m[a] - delm[a]
        else:
            m[a] = m[a] - delm[a] + delm[a+1]

        sigma[a] = m[a]/deltaA[a]
        w[a] = w_new[a]